import pytest
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.options.ios import XCUITestOptions

# 初始化“全局变量”
@pytest.fixture(scope="session")
def global_variable():
    global_var = {'version': '1.0'}
    return global_var

def pytest_addoption(parser):
    parser.addoption("--platform_name", action="store",help="Platform name for Appium.")
    parser.addoption("--automation_name", action="store",help="Automation name for Appium.")
    parser.addoption("--device_name", action="store",help="Device name for Appium.")
    parser.addoption("--app_package_name", action="store",help="Application package name.")
    parser.addoption("--app_activity_name", action="store", help="Application activity name.")
    parser.addoption("--app_url", action="store",help="URL/path to the application APK.")
    parser.addoption("--webdriver_url_appium", action="store",help="URL/path to the webdriver_url_appium.")
    parser.addoption("--allure_reports", action="store",help="URL/path to the allure_reports.")
    parser.addoption("--allure_reports_url", action="store",help="URL/path to the allure_reports_url.")
    parser.addoption("--allure_reports_url_port", action="store", help="URL/path to the allure_reports_url_port.")
    
#初始化： 测试函数引用driver打开设备应用时不重装应用但需要清空应用所有数据时用这个
@pytest.fixture(scope="module") 
def driver(request):
    desired_caps = {
        'platformName': request.config.getoption('--platform_name'),  # ios、Android
        'automationName': request.config.getoption('--automation_name'), # ios:XCUITest;  android :UiAutomator2
        # 'platformVersion': request.config.getoption('--platform_version'),
        # 'newCommandTimeout': request.config.getoption('--new_command_timeout'),
        'deviceName': request.config.getoption('--device_name'),
        # 'app': request.config.getoption('--app_url'),
        'fullReset': False,   #重装应用
        'fastReset': False,  #清数据
        'noReset': True,  #不重置

        #android
        'appPackage':  request.config.getoption('--app_package_name'),  #com.tencent.mm
        'appActivity': request.config.getoption('--app_activity_name'),  # mp:
        'autoGrantPermissions': True,
        'allowInsecure': ['adb_shell'] ,

        #ios
        'bundleId': request.config.getoption('--bundle_id'),  #"com.ut.pc.UTengineFrameworkTest"
        'xcodeOrgId': request.config.getoption('--xcode_org_id'), #team ID
        'xcodeSigningId': request.config.getoption('iPhone Developer'),
        'udid': request.config.getoption('--udid'),
    }
    if request.config.getoption('--platform_name')=="Android" :
        options = UiAutomator2Options().load_capabilities(desired_caps)
        options.set_capability('allowInsecure', ['adb_shell'])
        driver = webdriver.Remote(request.config.getoption('--webdriver_url_appium'), options=options)
        driver.implicitly_wait(5)
    elif  request.config.getoption('--platform_name')=="ios" : 
        options = XCUITestOptions().load_capabilities(desired_caps)
        options.set_capability('allowInsecure', ['adb_shell'])
        driver = webdriver.Remote(request.config.getoption('--webdriver_url_appium'), options=options)
        driver.implicitly_wait(5)
    yield driver  
    driver.quit()



# options = UiAutomator2Options().load_capabilities(desired_caps)
# options = XCUITestOptions().load_capabilities(desired_caps)

# driver = webdriver.Remote(request.config.getoption('--webdriver_url_appium'), options=options)
